package com.leyou.auth.interceptors;

import com.leyou.auth.constants.JwtConstants;
import com.leyou.auth.dto.Payload;
import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.auth.utils.UserContext;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.CookieUtils;
import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@Slf4j
public class LoginInterceptors implements HandlerInterceptor {
    private final JwtUtils jwtUtils;

    public LoginInterceptors(JwtUtils jwtUtils) {
        this.jwtUtils = jwtUtils;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        try {
            //获取cookie
            String cookieValue = CookieUtils.getCookieValue(request, JwtConstants.COOKIE_NAME);
            //获取jwt
            Payload payload = jwtUtils.parseJwt(cookieValue);
            //获取用户
            UserDetail userDetail = payload.getUserDetail();
            log.info("用户{}正在访问。",userDetail.getUsername());
            UserContext.setUser(userDetail);
            return true;
        }catch (JwtException e){
            throw new LyException(401,"JWT无效或过期！",e);
        }catch (IllegalArgumentException e){
            throw new LyException(401,"用户未登录!",e);
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //业务结束后，移除用户
        UserContext.removeUser();
    }
}
